#include "stm32f10x.h"                  // Device header

void IC_Init(void)//输入捕获初始化函数
{
    //配置外设时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

   	//配置GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入模式
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//配置定时器
	TIM_InternalClockConfig(TIM3);//定时器时钟源为RCC内部时钟
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period=65535;//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler= 10-1;//PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x00;
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//时基单元配置完成
	//公式：采样标准频率fc=72000000/(PSC+1);实测频率fx=fc/N
	
	//配置输入捕获单元
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
	TIM_ICInitStructure.TIM_ICFilter=0x1;
	TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
	TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
	TIM_ICInitStructure.TIM_ICSelection= TIM_ICSelection_DirectTI;
	TIM_ICInit(TIM3,&TIM_ICInitStructure);
	TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);//打开PWMI模式
	
	//配置主从触发模式
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);//设置触发源
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);//设置从模式
	
	//万事俱备，启动定时器
	TIM_Cmd(TIM3,ENABLE);
}

uint32_t get_freq(void)
{    
  return 7200000/(TIM_GetCapture1(TIM3)+1);
}

uint32_t get_dutycycle(void)
{    
  return TIM_GetCapture2(TIM3)*100/TIM_GetCapture1(TIM3);
}





